<?php

namespace App\Models;

use Illuminate\Database\Eloquent\Model;
use Illuminate\Pagination\LengthAwarePaginator;
use Illuminate\Support\Facades\Request;
use Illuminate\Support\Facades\DB;


class SalesCount extends Model
{
    public function paginate()
    {
        $cycle = Request::get('cycle') ? Request::get('cycle') : 'date_str';
        //dump($cycle);exit;


        $perPage = Request::get('per_page', 10);

        $page = Request::get('page', 1);

        $start = ($page-1)*$perPage;

        $result = DB::table('sales_counts')
            //->join('users','orders.seller_id','=','users.id')
            ->groupBy($cycle)
            ->get(
                array(
                    DB::raw('SUM(money) as money'),
                    DB::raw($cycle.' as cycle'),
                )
            )->toArray();


        $movies = static::hydrate($result);

        $paginator = new LengthAwarePaginator($movies, $start, $perPage);

        $paginator->setPath(url()->current());

        return $paginator;
    }

    public static function with($relations)
    {
        return new static;
    }
}
